//https://leetcode.cn/problems/reverse-string-ii/

void swap(char& x, char& y)
{
    char temp = x;
    x = y;
    y = temp;
}

class Solution {
public:
    string reverseStr(string s, int k) {
        if (k == 1)
            return s;

        for (int i = 0; i < s.size(); i += 2 * k)
        {
            int left = i;
            int right = i + k - 1 < s.size() - 1 ? i + k - 1 : s.size() - 1;
            while (left <= right)
            {
                swap(s[left], s[right]);
                left++;
                right--;
            }
        }

        return s;
    }

};